package tianrun.ziguan.api.calculate.config.service.cal.asset.impl;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import tianrun.ziguan.api.calculate.config.config.influxdb.InfluxDbName;
import tianrun.ziguan.api.calculate.config.dto.excel.TimeValueImport;
import tianrun.ziguan.api.calculate.config.dto.request.AddDataParam;
import tianrun.ziguan.api.calculate.config.service.influxdb.IInfluxDbService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TimeValueListener extends AnalysisEventListener<TimeValueImport> {
    private final List<TimeValueImport> list = new ArrayList<>();
    private final IInfluxDbService influxDbService;
    private final String label;
    private final String assetNum;

    public TimeValueListener(IInfluxDbService influxDbService, AddDataParam param) {
        this.influxDbService = influxDbService;
        this.label = param.getLabel();
        this.assetNum = param.getAssetNum();

    }

    @Override
    public void invoke(TimeValueImport o, AnalysisContext analysisContext) {
        list.add(o);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        handle();
    }

    public void handle() {
        influxDbService.insertBatch(InfluxDbName.SOURCE_HISTORY, InfluxDbName.SOURCE_HISTORY_DB_NAME, list.stream().map(timeValueImport -> timeValueImport.of(label, assetNum)).collect(Collectors.toList()));
    }


}
